{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 问题\n",
    "$$\n",
    "\\begin{align}\n",
    "\\min \\quad & 2 x_1^2 + x_2^2 + x_1 x_2 + x_1 +x_2\n",
    "\\\\\n",
    "s.t. \\quad & x_1 + x_2 = 1\n",
    "\\\\\n",
    "& 0 < x_1 < 0.7\n",
    "\\\\\n",
    "& 0 < x_2 < 0.7\n",
    "\\end{align}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "# 转化\n",
    "$$\n",
    "\\begin{align}\n",
    "\\min \\quad & \\frac{1}{2}\n",
    "\\begin{bmatrix}\n",
    "    x_1 \\\\\n",
    "    x_2\n",
    "\\end{bmatrix}^T\n",
    "\n",
    "\\begin{bmatrix}\n",
    "    4 & 1 \\\\\n",
    "    1 & 2\n",
    "\\end{bmatrix}\n",
    "\n",
    "\\begin{bmatrix}\n",
    "    x_1 \\\\\n",
    "    x_2\n",
    "\\end{bmatrix}\n",
    " + \n",
    "\\begin{bmatrix}\n",
    "    1 \\\\\n",
    "    1\n",
    "\\end{bmatrix}^T\n",
    "\\begin{bmatrix}\n",
    "    x_1 \\\\\n",
    "    x_2\n",
    "\\end{bmatrix}\n",
    "\n",
    "\n",
    "\\\\\n",
    "s.t. \\quad & \n",
    "\\begin{bmatrix}\n",
    "    1 \\\\\n",
    "    0 \\\\\n",
    "    0\n",
    "\\end{bmatrix}\n",
    "\\leq\n",
    "\\begin{bmatrix}\n",
    "    1 & 1\\\\\n",
    "    1 & 0\\\\\n",
    "    0 & 1\n",
    "\\end{bmatrix}\n",
    "\\begin{bmatrix}\n",
    "    x_1 \\\\\n",
    "    x_2\n",
    "\\end{bmatrix}\n",
    "\\leq\n",
    "\\begin{bmatrix}\n",
    "    1 \\\\\n",
    "    0.7 \\\\\n",
    "    0.7\n",
    "\\end{bmatrix}\n",
    "\\end{align}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-----------------------------------------------------------------\n",
      "           OSQP v0.6.2  -  Operator Splitting QP Solver\n",
      "              (c) Bartolomeo Stellato,  Goran Banjac\n",
      "        University of Oxford  -  Stanford University 2021\n",
      "-----------------------------------------------------------------\n",
      "problem:  variables n = 2, constraints m = 3\n",
      "          nnz(P) + nnz(A) = 7\n",
      "settings: linear system solver = qdldl,\n",
      "          eps_abs = 1.0e-03, eps_rel = 1.0e-03,\n",
      "          eps_prim_inf = 1.0e-04, eps_dual_inf = 1.0e-04,\n",
      "          rho = 1.00e-01 (adaptive),\n",
      "          sigma = 1.00e-06, alpha = 1.00, max_iter = 4000\n",
      "          check_termination: on (interval 25),\n",
      "          scaling: on, scaled_termination: off\n",
      "          warm start: on, polish: off, time_limit: off\n",
      "\n",
      "iter   objective    pri res    dua res    rho        time\n",
      "   1  -4.9384e-03   1.00e+00   2.00e+02   1.00e-01   4.13e-04s\n",
      "  50   1.8800e+00   1.91e-07   7.50e-07   1.38e+00   9.69e-04s\n",
      "\n",
      "status:               solved\n",
      "number of iterations: 50\n",
      "optimal objective:    1.8800\n",
      "run time:             1.37e-03s\n",
      "optimal rho estimate: 1.36e+00\n",
      "\n",
      "[0.3 0.7]\n"
     ]
    }
   ],
   "source": [
    "import osqp\n",
    "import numpy as np\n",
    "from scipy import sparse\n",
    "\n",
    "# Define problem data\n",
    "P = sparse.csc_matrix([[4, 1], [1, 2]])\n",
    "q = np.array([1, 1])\n",
    "A = sparse.csc_matrix([[1, 1], [1, 0], [0, 1]])\n",
    "l = np.array([1, 0, 0])\n",
    "u = np.array([1, 0.7, 0.7])\n",
    "\n",
    "# Create an OSQP object\n",
    "prob = osqp.OSQP()\n",
    "\n",
    "# Setup workspace and change alpha parameter\n",
    "prob.setup(P, q, A, l, u, alpha=1.0)\n",
    "\n",
    "# Solve problem\n",
    "res = prob.solve()\n",
    "\n",
    "print(np.round(res.x[:2], 4))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.7.3 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "cdb1d994209fd2338bbe5fcfbb0cc42bfd775094d7edf1f7a9e3c177245b69c5"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
